// JavaScript Document
(function(window) {
    /***
    *定义变量
    ***/
    var $ = window.jQuery;

    // -------------------------- 辅助函数 -------------------------- //
    // extend objects 扩展对象
    function extend(a, b) {
        for (var prop in b) {
            a[prop] = b[prop];
        }
        return a;
    }

    // -------------------------- 下定义函数 -------------------------- //
    function defineweixinjs() {
        /**
        返回函数
        **/
        var weixinjs = {};

        /**
		自定义分享描述文字
        **/
        weixinjs.WeixinJSBridgeReady = function(options) {
            var defaultOptions = {
                imgUrl: "",
                //缩略图地址
                fLink: document.URL,
                //好友连接地址
                tLink: document.URL,
                //朋友圈连接地址
                wLink: document.URL,
                //微博连接地址
                fTitle: document.title,
                //好友标题
                fContent: "",
                //好友content
                tTitle: document.title,
                //朋友圈标题
                tContent: "",
                //朋友圈content
                wContent: "",
                //微博content
            };
            var _options = extend(defaultOptions, options);

            document.addEventListener('WeixinJSBridgeReady',
            function onBridgeReady() {
                //发送给好友
                WeixinJSBridge.on('menu:share:appmessage',
                function(argv) {
                    WeixinJSBridge.invoke('sendAppMessage', {
                        "img_url": _options.imgUrl,
                        "img_width": "640",
                        "img_height": "640",
                        "link": _options.fLink,
                        "desc": _options.fContent,
                        "title": _options.fTitle
                    },
                    function(res) {
                        _report('send_msg', res.err_msg);
                    })
                });

                // 分享到朋友圈
                WeixinJSBridge.on('menu:share:timeline',
                function(argv) {
                    WeixinJSBridge.invoke('shareTimeline', {
                        "img_url": _options.imgUrl,
                        "img_width": "640",
                        "img_height": "640",
                        "link": _options.tLink,
                        "desc": _options.tContent,
                        "title": _options.tTitle
                    },
                    function(res) {
                        _report('timeline', res.err_msg);
                    });
                });

                // 分享到微博
                WeixinJSBridge.on('menu:share:weibo',
                function(argv) {
                    WeixinJSBridge.invoke('shareWeibo', {
                        "content": _options.wContent,
                        "url": _options.wLink,
                    },
                    function(res) {
                        _report('weibo', res.err_msg);
                    });
                });

            },
            false);
        };
		
        /**
		隐藏微信中网页右上角按钮
		**/
        weixinjs.optionMenu = function(value) {
            if (typeof value !== 'boolean') return;
            var option = value ? 'showOptionMenu': 'hideOptionMenu';
            document.addEventListener('WeixinJSBridgeReady',
            function onBridgeReady() {
                WeixinJSBridge.call(option);
            });
        };

        /**
		隐藏微信中网页底部导航栏
		**/
        weixinjs.toolbar = function(value) {
            if (typeof value !== 'boolean') return;
            var option = value ? 'showToolbar': 'hideToolbar';
            document.addEventListener('WeixinJSBridgeReady',
            function onBridgeReady() {
                WeixinJSBridge.call(option);
            });
        };

        /**
		网页获取用户网络状态
		**/
        weixinjs.getNetworkType = function() {
            document.addEventListener('WeixinJSBridgeReady',
            function onBridgeReady() {
                WeixinJSBridge.invoke('getNetworkType', {},
                function(e) {
                    WeixinJSBridge.log(e.err_msg);
                    alert(e.err_msg);
                });
            });
        };

        return weixinjs;
    }

    window.weixinjs = defineweixinjs();
})(window);